package com.jz.community.moduleshoppingguide.nearshop.ui.view;


import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jz.community.basecomm.utils.SHelper;
import com.jz.community.moduleshoppingguide.R;


public class ShopCarView extends LinearLayout {
    private TextView car_limit, tv_amount, tv_self, car_non_select;
    public ImageView iv_shop_car;
    public TextView car_badge;
    public TextView cart_tips_tv;
    public LinearLayout amount_container;
    private RelativeLayout tips_rl;
    public View shoprl;
    public int[] carLoc;
    private boolean haveGoods = false;

    private Context context;

    private int total_num = 0;


    public interface OnAccountClick {

        void onAccountClick(int type);
    }

    private OnAccountClick onAccountClick;

    public void setOnAccountClick(OnAccountClick onAccountClick) {
        this.onAccountClick = onAccountClick;
    }

    public ShopCarView(@NonNull Context context) {
        super(context);
        this.context = context;
        initView();
    }

    public ShopCarView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initView();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        carLoc = new int[2];
        iv_shop_car.getLocationInWindow(carLoc);
        carLoc[0] = carLoc[0] + iv_shop_car.getWidth() / 2 - SHelper.dp2px(getContext(), 10);
    }

    private void initView() {
        setClipChildren(false);
        View rootView = LayoutInflater.from(context).inflate(R.layout.view_car, this);
        tips_rl = (RelativeLayout) rootView.findViewById(R.id.cart_tips_rl);
        iv_shop_car = (ImageView) rootView.findViewById(R.id.iv_shop_car);
        car_badge = (TextView) rootView.findViewById(R.id.car_badge);
        car_limit = (TextView) rootView.findViewById(R.id.car_limit);
        tv_amount = (TextView) rootView.findViewById(R.id.tv_amount);
        cart_tips_tv = (TextView) rootView.findViewById(R.id.cart_tips_tv);
        tv_self = (TextView) rootView.findViewById(R.id.tv_self);
        car_non_select = (TextView) rootView.findViewById(R.id.car_non_select);
        amount_container = (LinearLayout) rootView.findViewById(R.id.amount_container);
        shoprl = rootView.findViewById(R.id.car_rl);
        iv_shop_car.setOnClickListener(new toggleCar());
        car_non_select.setVisibility(View.GONE);
        amount_container.setVisibility(View.GONE);
    }

    public void updateCartData(String price, int nums, int isRest, String send_type) {
        if (nums == 0) {
            //购物车中无商品
            car_non_select.setVisibility(View.VISIBLE);
            amount_container.setVisibility(View.GONE);
            tv_self.setTextColor(Color.parseColor("#989898"));
            tv_amount.setTextColor(Color.parseColor("#989898"));
            car_limit.setBackgroundColor(Color.parseColor("#989898"));
            car_limit.setEnabled(false);
            iv_shop_car.setImageResource(R.mipmap.shop_car_empty);
            haveGoods = false;
        } else {
            //购物车中有商品
            car_non_select.setVisibility(View.GONE);
            amount_container.setVisibility(View.VISIBLE);
            tv_self.setTextColor(Color.parseColor("#f2383b"));
            tv_amount.setTextColor(Color.parseColor("#f2383b"));
            car_limit.setBackgroundColor(Color.parseColor("#f2383b"));
            car_limit.setEnabled(true);
            iv_shop_car.setImageResource(R.mipmap.shop_car_select);
            car_limit.setOnClickListener(v -> {
                if (null != onAccountClick) {
                    onAccountClick.onAccountClick(2);
                }
            });
            haveGoods = true;
        }
        //0 休息 1 营业
        if (isRest == 0) {
            //休息中
            tips_rl.setVisibility(VISIBLE);
        } else if (isRest == 1) {
            tips_rl.setVisibility(GONE);
        }
        if ("2".equals(send_type)) {
            tv_self.setText("仅支持到店自提");
            if (isRest == 0) {
                cart_tips_tv.setText("店铺休息中，下单后请在营业时间提货");
            }
        } else if ("1".equals(send_type)) {
            tv_self.setText("仅支持配送");
            if (isRest == 0) {
                cart_tips_tv.setText("店铺休息中，下单后我们将尽快为您配送");
            }
        } else if ("0".equals(send_type)) {
            tv_self.setText("支持配送与自提");
            if (isRest == 0) {
                cart_tips_tv.setText("店铺休息中，下单后我们将尽快为您配送");
            }
        } else {
            tv_self.setText("仅支持到店自提");
            if (isRest == 0) {
                cart_tips_tv.setText("店铺休息中，下单后请在营业时间提货");
            }
        }
        tv_amount.setText("¥" + price);

    }

    public void showBadge(int total) {
        total_num = total;
        if (total > 0) {
            car_badge.setVisibility(View.VISIBLE);
            car_badge.setText(total + "");
        } else {
            car_badge.setVisibility(View.INVISIBLE);
        }
    }

    public boolean isHaveGoods() {
        return haveGoods;
    }

    public int getNum() {
        return total_num;
    }

    //点击进入购物车
    private class toggleCar implements OnClickListener {

        @Override
        public void onClick(View view) {
            if (null != onAccountClick) {
                onAccountClick.onAccountClick(1);
            }
        }
    }


}
